# -*- coding: utf-8 -*- 
import requests
from bs4 import BeautifulSoup
# http://www.beqege.cc/
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0',
    'connection': 'keep-alive',
    'host': 'www.beqege.cc',
    'referer': 'https://www.beqege.cc/38153/',
    'Cookie': 'cf_clearance=EoXh66egpfjNykZw4Vil.6b9gGJkYghuMnqwXM.FrBg-1740031001-1.2.1.1-5UC.yMZYOp0WJ4Ss9t7iN_4KXW1z4moHXio4xLW81.41E_SLSraqVmVHsp7uC_hyJ2CbiBft_vQ482KUpwRlfeRWo5dKp.m3aEbK7hsKrut8IjHjsvO.x2eruWrhZ3tM3i.ho_IYAW_UwG09XnmUEYMavA_mzmAtbXV9DcFlGeD9lp0odh7uZyeDtYvv1m_DVkSCMeM_ilu.gr.GS7130mXOQSkKJQrPwgDu4r59Uxbpqlk9EWn1pHWnsJuUw9YhEa9KKRiEOseRn.ojnuOub1edExCKxmdj4htA7FSiS8m7UcnJmYkH8FN7cFo_4kBYIz52Jj3haphnVB.oILYWXA',
}

def get_download(url, novel):
    try:
        with requests.get(url, headers=headers) as req:
            req.encoding = 'utf8'
            html = req.text
            data = BeautifulSoup(html, "html.parser")
            # Get the chapter name
            s = data.title.string
            r = s.split('_')
            section_name = r[0]
            print(url, section_name)
            # Get the chapter content
            section_text = data.select('#content')[0].text
            # Write to file
            with open(novel, 'a', encoding='utf-8') as f:
                f.write(section_name + "\n")
                f.write(section_text + "\n")
            return "ok"
    except requests.RequestException as e:
        print(f"Request error: {e}")
    except IndexError as e:
        print(f"Index error: {e}")
    except Exception as e:
        print(f"Unexpected error: {e}")
    return url

if __name__ == '__main__': 
    x = "https://www.beqege.cc/16751/22334"
    nurl = "ok"
    novel = '雪中悍刀行.txt'    
    num = 997  # Total number of chapters
    print(novel, "Total " + str(num) + " chapters, starting download")
    for i in range(1, num + 1):
        if nurl == "ok":
            url = x + str(i) + ".html"
        nurl = get_download(url, novel)